<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>创建请求</title>
    <link rel="stylesheet" href="css/select2.css">
</head>
<body>
<select name="all-req" id="all-req" ></select><span id="req-desc"></span><br>
<button class="button" id="get-req-param">获取参数</button>
<form id="req-params"></form>
<button id="send-req" type="button" style="display: none">发起请求</button>
<button id="send-save" type="button" style="display: none">保存为快捷</button>
<script src="lib/jquery-1.11.3.min.js" crossorigin></script>
<script src="lib/select2.min.js" crossorigin></script>
<script src="js/storage.js" crossorigin></script>
<script src="js/form.js" crossorigin></script>
<script src="js/main.js" crossorigin></script>
<script>
;!function(){
    gniGetAllReq().then((data) => {
        data = $.parseJSON(data)
        if (data.Code === 0 && data.Data) {
            for (let k in data.Data) {
                $('#all-req').append($('<option>', {value:k, text:k}))
            }
            $('#send-req').hide()
            $('#send-save').hide()
            $('#all-req').select2();
        }
    })


    $('#get-req-param').on('click', function () {
        let req = $('#all-req').val()
        $('#req-desc').text(req)
        gniGetReqFields(req).then(data => {
            $('#req-params').children().remove()
            data = $.parseJSON(data)
            if (data.Code === 0) {
                for (let k in data.Data) {
                    console.log(createInput(k + '(' + data.Data[k] + ')', ''))
                    $('#req-params').append(createInput(k, '', k + '(' + data.Data[k] + ')'))
                }
                $('#send-req').show()
                $('#send-save').show()
            }
        })
    })
    $('#send-req').on('click', function () {
        let params = formToJson($('#req-params'))
        gniSendReq($('#all-req').val(), params).then(data => {
            console.log(data)
        })
    })
    $('#send-save').on('click', function () {
        let rs = GetReqShortcuts()
        let idx = $('#all-req').val()
        rs[idx] = FormToJsonObj($('#req-params'))
        read(idx, function (rst) {
            if (rst) {
                put(idx, FormToJsonObj($('#req-params')), function () {
                    refreshReqShortcuts($('#op-ext', window.parent.document))
                })
            } else {
                add(idx, FormToJsonObj($('#req-params')), function () {
                    refreshReqShortcuts($('#op-ext', window.parent.document))
                })
            }
        })
        localStorage.setItem("REQShortcut", JSON.stringify(rs))
        refreshReqShortcuts($('#op-ext', window.parent.document))
    })
}()

</script>
</body>
</html>